package com.bayair.doctor.ui.splash;

import android.content.Intent;

import com.bayair.doctor.R;
import com.bayair.doctor.biz.helper.SpSetting;
import com.bayair.doctor.biz.im_helper.IMHelper;
import com.bayair.doctor.ui.login.LoginActivity;
import com.bayair.doctor.ui.main.MainActivity;
import com.blankj.utilcode.util.NetworkUtils;

public class SplashActivity extends BaseSplashActivity {


    private boolean isLogin;

    @Override
    protected long getSplashDurationMillis() {
        return 1000;
    }

    @Override
    protected void onCreateActivity() {
        initSplashView(R.drawable.splash_theme_bg);
        startSplash(true);
    }

    @Override
    protected void onSplashFinished() {
        isLogin = SpSetting.isLogin();
        if (isLogin) {
            if (!NetworkUtils.isConnected()) {
                toast("网络不给力，请检查网络设置。");
                goToMain();
                return;
            }

            //登录IM
            IMHelper.autoLoginIM(new IMHelper.LoginListener() {
                @Override
                public void onError() {
                    goToLogin();
                }

                @Override
                public void onSuccess() {
                    goToMain();
                }
            });
        } else {
            goToLogin();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }


    private void goToMain() {
        startActivity(new Intent(this, MainActivity.class));
    }

    private void goToLogin() {
        startActivity(new Intent(this, LoginActivity.class));
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(android.R.anim.fade_in, 0);
    }

}
